public class SolutionOfPalindrome {
    public static void main(String[] args) {
        String s="qabcbag";
        SolutionOfPalindrome sop=new SolutionOfPalindrome();
        System.out.println(sop.longestPalindrome(s));
        
    }

    public String longestPalindrome(String s){
        int len=s.length();
        if(len<2)
            return s;
        char[] chars = s.toCharArray();
        boolean[][] dp=new boolean[len][len];
        int startIndex=0,maxlen=0;
        for(int j=1;j<len;j++){//j和i分别是左右指针
            for(int i=0;i<j;i++){
                if(chars[i]!=chars[j])
                    dp[i][j]=false;
                else {
                    if(j-i<3) dp[i][j]=true;
                    else dp[i][j]=dp[i+1][j-1];
                }
                if(dp[i][j]==true&&j-i+1>maxlen){
                    startIndex=i;
                    maxlen=j-i+1;
                }
            }
        }
        return s.substring(startIndex,startIndex+maxlen);
    }
}
